<?php

namespace app\admin\controller\m;

use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\response\Json;

/**
 * 物料仓储
-采购调拨
 *
 * @icon fa fa-circle-o
 */
class Stocksale extends Backend
{

    /**
     * Stockbill模型对象
     * @var \app\admin\model\m\Stockbill
     */
    protected $model = null;
    protected $apart = '销售';

    //  大厂权限      @wtt
    protected $dataLimitCom = "auth";               // 默认: false,  'auth':  强制验证厂区权限 ( 表示显示当前厂区数据 )
    protected $dataCompanyField = "factory_id";     // 厂区权限字段

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\m\Stockbill;
        $this->view->assign("calList", $this->model->getCalList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        // id="c-admin_ids" data-source="auth/admin/index" data-params='{"custom[factory_id]":"1"}'  data-field="nickname"

        // ok
        // 添加筛选条件,  限制本厂管理员,  方法 selectpage

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->where("type", $this->apart )
            ->order($sort, $order)
            ->paginate($limit);

        $list = $this->getXXX($list);

//        print_r($list->items() ); die;
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

}
